<?php

/**
 * Class ProfileController
 */
class ProfileController extends BaseController{
    /**
     * 前函数
     */
     public function init(){
         parent::init();
         if(LoginHelper::getUserName() == '' || LoginHelper::getUserId() == 0){
             $this -> redirect($this -> _createUrl('login/index'));
         }
        return true;
     }

     public function editAction(){
         $htmls = array();
         $editor = $this -> module('kindeditor');
         $htmls['s_url'] = $editor -> get_all_editor('s_url').'<textarea name="s_url" id="s_url" style="width:700px;height:400px;"></textarea>';
         $this->assign('htmls',$htmls);
         $model = Controller::createMode(BaseController::$default_db,'t_26_suppliers');
         $s_id=LoginHelper::getUserId();
         $s_info = $model->getOne(array('s_id'=>$s_id ));
         $this->assign('s_info',$s_info);
         $this -> display();
     }

    /**
     * 更新商家信息
     */
    public function updateInfoAction(){
        $model = Controller::createMode(BaseController::$default_db,'t_26_suppliers');
        $s_id=LoginHelper::getUserId();
        $s_name = $this->get("s_name") ? $this->get("s_name")  : $this->post("s_name") ;
        $s_addr = $this->get("s_addr") ? $this->get("s_addr")  : $this->post("s_addr") ;
        $s_tel= $this->get("s_tel") ? $this->get("s_tel")  : $this->post("s_tel") ;
        $s_image= $this->get("s_image") ? $this->get("s_image")  : $this->post("s_image") ;
        $s_url = $this->get("s_url") ? $this->get("s_url")  : $this->post("s_url") ;
        $s_bg_image = $this->get("s_bg_image") ? $this->get("s_bg_image")  : $this->post("s_bg_image") ;
        $params=array(
            's_name'=>$s_name,
            's_addr'=>$s_addr,
            's_tel'=>$s_tel,
            's_image'=>$s_image,
            's_url'=>urldecode($s_url),
            's_bg_image'=>$s_bg_image,
        );
        $result = $model->update($params," s_id=$s_id");
        if($result){
            $result = array();
            $result['code'] = 200;
            $result['msg'] = '更新成功';
            echo json_encode($result, true) ;exit;
        } else {
            $result = array();
            $result['code'] = 203;
            $result['msg'] = '更新失败';
            echo json_encode($result, true) ;exit;
        }

    }

    public function passwordAction(){

        $this -> display();
    }

    public function modifyPwdAction(){

        $model = Controller::createMode(BaseController::$default_db,'t_26_suppliers');
        $s_id=LoginHelper::getUserId();
        $s_info = $model->getOne(array('s_id'=>$s_id ));
        $s_oldPwd = $this->get("oldPwd") ? $this->get("oldPwd")  : $this->post("oldPwd") ;
        $s_newPwd = $this->get("newPwd") ? $this->get("newPwd")  : $this->post("newPwd") ;
        $s_password=md5($s_oldPwd);
        $params=array(
            'login_password'=>md5($s_newPwd),
        );
        if($s_password==$s_info['login_password']){
            $result = $model->update($params," s_id=$s_id");
            if($result){
                $result = array();
                $result['code'] = 200;
                $result['msg'] = '更新成功';
                echo json_encode($result, true) ;exit;
            } else {
                $result = array();
                $result['code'] = 203;
                $result['msg'] = '更新失败';
                echo json_encode($result, true) ;exit;
            }
        }else{
            $result = array();
            $result['code'] = 202;
            $result['msg'] = '原密码错误';
            echo json_encode($result, true) ;exit;
        }
    }
}
